package com.hjm.common.utils;

import cn.hutool.core.util.IdUtil;
import com.alibaba.druid.util.StringUtils;
import com.google.common.collect.Maps;
import com.hjm.common.utils.request.AutoLoadData;
import com.hjm.framework.config.BlueConfig;
import com.yxhd.encrypt.EncryptUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.security.Key;
import java.security.MessageDigest;
import java.security.spec.KeySpec;
import java.sql.Clob;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 *********************************************************.<br>
 * [类] Utils <br>
 * [描述] 工具类 <br>
 * [参数] (对参数的描述) <br>
 * [时间] 2017-12-07 下午19:19:19 <br>
 *********************************************************.<br>
 */
@SuppressWarnings("all")
public class Utils {

	public static boolean isEmpty(String str){
		return str==null ||str.isEmpty();
	}


	/**
	 *
	 *********************************************************.<br>
	 * [方法] readCookieMap <br>
	 * [描述] 获取Cookie信息 <br>
	 * [参数] (对参数的描述) <br>
	 * [返回] String <br>
	 * [时间] 2017-12-07 下午19:19:19 <br>
	 * [作者] lvl
	 *********************************************************.<br>
	 */
	public static Map<String, Cookie> readCookieMap(HttpServletRequest request){
		Map<String, Cookie> cookieMap=new HashMap<String, Cookie>();
		Cookie[] cookies=request.getCookies();
		if (null!=cookies) {
			for (Cookie cookie : cookies) {
				cookieMap.put(cookie.getName(), cookie);
			}
		}
		return cookieMap;
	}
	/**
	 *
	 *********************************************************.<br>
	 * [方法] MD5 <br>
	 * [描述] MD5 加密 <br>
	 * [参数] (对参数的描述) <br>
	 * [返回] String <br>
	 * [时间] 2017-12-07 下午19:19:19 <br>
	 * [作者] lvl
	 *********************************************************.<br>
	 */
	public static String MD5(String str){
		char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
		try {
			byte[] itInput=str.getBytes();
			// 获得MD5摘要算法的 MessageDigest 对象
			MessageDigest md=MessageDigest.getInstance("MD5");
			//使用指定的字节更新摘要
			md.update(itInput);
			//获取密文
			byte[] mdMd5=md.digest();
			//把密文转换成十六进制的字符串形式
			int j=mdMd5.length;
			char[] chr=new char[j*2];
			int k=0;
			for (int i = 0; i < j; i++) {
				byte bt=mdMd5[i];
				chr[k++] = hexDigits[bt >>> 4 & 0xf];
				chr[k++] = hexDigits[bt & 0xf];
			}
			return new String(chr);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 *
	 *********************************************************.<br>
	 * [方法] addCookie <br>
	 * [描述] 设置COOKIE <br>
	 * [参数] (对参数的描述) <br>
	 * [返回] String <br>
	 * [时间] 2017-12-07 下午19:19:19 <br>
	 * [作者] lvl
	 *********************************************************.<br>
	 */
	public static void addCookie(HttpServletResponse response, String name,
                                 String value, int maxAge){
		Cookie cookie=new Cookie(name, value);
		cookie.setPath("/");
		if (maxAge>0) {
			cookie.setMaxAge(maxAge);
		}
		response.addCookie(cookie);
	}
	/**
	 *
	 *********************************************************.<br>
	 * [方法] formateDate <br>
	 * [描述] 格式化日期 <br>
	 * [参数] (对参数的描述) <br>
	 * [返回] String <br>
	 * [时间] 2017-12-07 下午19:19:19 <br>
	 * [作者] lvl
	 *********************************************************.<br>
	 */
	public static String formateDate(int style){
		String date="";
		String type="yyyyMMdd";//默认格式
		switch (style) {
			case 0:
				type="yyyy-MM-dd";
				break;
			case 1:
				type="yyyyMMddHHmmss";
				break;
			case 2:
				type="yyyy-MM-dd HH:mm:ss";
				break;
			case 3:
				type="HH:mm:ss";
				break;
			case 4:
				type="yyyyMMddHHmmssSSS";
				break;
			case 5:
				type="";
				break;
			case 6:
				type="MMdd";
				break;
		}
		SimpleDateFormat sdf=new SimpleDateFormat(type);
		date=sdf.format(new Date());
		return date;
	}

	/**
	 *********************************************************.<br>
	 * [方法] getWeight <br>
	 * [描述] 随机生成秘钥字符串 <br>
	 * [参数] 无参 <br>
	 * [返回] String <br>
	 * [时间] 2018年11月12日 下午2:49:46 <br>
	 *********************************************************.<br>
	 */
	public static String getWeight() {
		char[] str={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
		String weight="";
		for (int i = 0; i < 32; i++) {
			weight=weight+str[(int)(Math.random()*16)];
		}
		return weight;
	}

	/**
	 *
	 ********************************************************* .<br>
	 * [方法] bytesXOR <br>
	 * [描述] 字节数组异或 <br>
	 * [参数] 数据1、数据2 <br>
	 * [返回] byte[] <br>
	 * [时间] 2015-12-9 下午5:21:22 <br>
	 ********************************************************* .<br>
	 */
	public static byte[] bytesXOR(byte[] src, byte[] src1) throws Exception {
		int length = src.length;
		if (length != src1.length) {
			return null;
		}
		byte[] result = new byte[length];
		for (int i = 0; i < length; i++) {
			result[i] = byteXOR(src[i], src1[i]);
		}
		return result;
	}

	/**
	 *
	 ********************************************************* .<br>
	 * [方法] byteXOR <br>
	 * [描述] 字节异或 <br>
	 * [参数] 数据1、数据2 <br>
	 * [返回] byte <br>
	 * [时间] 2015-12-9 下午5:21:11 <br>
	 ********************************************************* .<br>
	 */
	private static byte byteXOR(byte src, byte src1) throws Exception {
		return (byte) ((src & 0xFF) ^ (src1 & 0xFF));
	}

	/**
	 *
	 ********************************************************* .<br>
	 * [方法] encrypt <br>
	 * [描述] 3des加密 <br>
	 * [参数] mab,key <br>
	 * [返回] byte[] <br>
	 * [时间] 2014-11-17 下午2:04:31 <br>
	 ********************************************************* .<br>
	 */
	public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
		byte newkey[] = new byte[24];
		System.arraycopy(key, 0, newkey, 0, key.length);
		System.arraycopy(key, 0, newkey, 16, 8);
		Key k = toKey(newkey); // ---这里卡了一下、百度一下可以优化吗
		Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
		cipher.init(Cipher.ENCRYPT_MODE, k);
		return cipher.doFinal(data);
	}

	/**
	 *
	 ********************************************************* .<br>
	 * [方法] deciphering <br>
	 * [描述] 3des解密 <br>
	 * [参数] mab,key <br>
	 * [返回] byte[] <br>
	 * [时间] 2014-11-17 下午2:04:49 <br>
	 ********************************************************* .<br>
	 */
	public static byte[] deciphering(byte[] data, byte[] key) throws Exception {
		byte newkey[] = new byte[24];
		System.arraycopy(key, 0, newkey, 0, key.length);
		System.arraycopy(key, 0, newkey, 16, 8);
		Key k = toKey(newkey);
		Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
		cipher.init(Cipher.DECRYPT_MODE, k);
		return cipher.doFinal(data);
	}

	private static SecretKey toKey(byte[] key) throws Exception {
		KeySpec dks = new DESedeKeySpec(key); // 这里改为了 DESedeKeySpec
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
		return keyFactory.generateSecret(dks);
	}

	/**
	 *
	 ********************************************************* .<br>
	 * [方法] byte2HexStr <br>
	 * [描述] bytes转换成十六进制字符串 <br>
	 * [参数] byte数组 <br>
	 * [返回] String <br>
	 * [时间] 2015-12-7 下午5:44:10 <br>
	 ********************************************************* .<br>
	 */
	public static String byte2HexStr(byte[] b) throws Exception {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			hs = appendField(hs,(stmp.length() == 1) ? appendField("0", stmp): stmp);
		}
		return hs.toUpperCase();
	}

	/**
	 *
	 ********************************************************* .<br>
	 * [方法] hexStr2Bytes <br>
	 * [描述] 十六进制字符串转换成bytes <br>
	 * [参数] 16进制字符串 <br>
	 * [返回] byte[] <br>
	 * [时间] 2015-12-7 下午5:44:43 <br>
	 ********************************************************* .<br>
	 */
	public static byte[] hexStr2Bytes(String src) throws Exception {
		int m = 0, n = 0;
		int l = src.length() / 2;
		byte[] ret = new byte[l];
		for (int i = 0; i < l; i++) {
			m = i * 2 + 1;
			n = m + 1;
			ret[i] = uniteBytes(src.substring(i * 2, m), src.substring(m, n));
		}
		return ret;
	}

	private static byte uniteBytes(String src0, String src1) throws Exception {
		byte b0 = Byte.decode(appendField("0x", src0)).byteValue();
		b0 = (byte) (b0 << 4);
		byte b1 = Byte.decode(appendField("0x", src1)).byteValue();
		byte ret = (byte) (b0 | b1);
		return ret;
	}

	/**
	 *
	 ********************************************************* .<br>
	 * [方法] appendField <br>
	 * [描述] 拼接字符串 <br>
	 * [参数] 参数1，参数2 <br>
	 * [返回] String <br>
	 * [时间] 2016-3-10 上午10:43:53 <br>
	 ********************************************************* .<br>
	 */
	public static String appendField(String one, String two) {
		return new StringBuffer(one).append(two).toString();
	}

	/**
	 *********************************************************.<br>
	 * [方法] writeTxt <br>
	 * [描述] 写入txt文本 <br>
	 * [参数] 目标地址、内容 <br>
	 * [返回] void <br>
	 * [时间] 2018年11月13日 上午11:20:28 <br>
	 *********************************************************.<br>
	 */
	public static void writeTxt(String filePath,String content) throws Exception {
		BufferedWriter out=null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath,true)));
			out.write(content+"\r\n");
		} catch (Exception e) {
			throw e;
		}finally{
			if(out!=null){
				out.close();
			}
		}
	}
	/**
	 *********************************************************.<br>
	 * [方法] delFile <br>
	 * [描述] 删除文件 <br>
	 * [参数] 目标地址、内容 <br>
	 * [返回] void <br>
	 * [时间] 2018年11月13日 上午11:20:28 <br>
	 *********************************************************.<br>
	 */
	public static boolean delFile(String path){
		boolean result=false;
		File file=new File(path);
		if(file.exists()&&file.isFile()){
			result=file.delete();
		}
		return result ;

	}

	/**
	 * 输出信息
	 * @param e
	 * @return
	 */
	public static String getAllErrorMessage(Exception e){
		StringWriter sw = null;
		PrintWriter pw = null;
		try {
			sw = new StringWriter();
			pw =  new PrintWriter(sw);
			//将出错的栈信息输出到printWriter中
			e.printStackTrace(pw);
			pw.flush();
			sw.flush();
		} finally {
			if (pw != null){
				pw.close();
			}
			if (sw != null) {
				try {
					sw.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}

		}

		return sw.toString();
	}

	/**
	 * 判断对象是否为空
	 * @param obj Object
	 * @return boolean
	 */
	public static boolean isNull(Object obj) {
		if(obj != null) {
			if (obj instanceof java.lang.String && "".equals(obj)){
				return true;
			}
			if (obj instanceof java.util.List){
				List list = (List)obj;
				if (list.size() == 0){
					return true;
				}
			}
			if (obj instanceof java.util.Map){
				Map map = (Map)obj;
				if (map.size() == 0){
					return true;
				}
			}
			if (obj instanceof java.util.Set){
				Set set = (Set)obj;
				if (set.size() == 0){
					return true;
				}
			}
			return false;
		}
		return true;
	}

	/**
	 * @Author 野猪佩奇
	 * @Description //删除文件
	 * @Date 17:44 2019/5/12
	 * @Param [path]
	 * @return boolean
	 **/
	public static boolean delAllFile(String path) {
		boolean flag = false;
		File file = new File(path);
		if (!file.exists()) {
			return flag;
		}
		if (!file.isDirectory()) {
			return flag;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			/*if (temp.isDirectory()) {
				delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
				flag = true;
			}*/
		}
		return flag;
	}
	/**
	 * @title 金额转换
	 * @param money
	 * @return
	 */
	public static String dealWithMoney(String money){
		String dealmoney="";
		String lastmoney="";
		if (money.contains(".")) {
			lastmoney=money.substring(money.indexOf(".")+1);
		}
		for (int i = lastmoney.length(); i < 2; i++) {
			money+="0";
		}
		if (money.contains(".")) {
			money=money.substring(0,money.indexOf(".")+3);
		}
		money=money.replace(".","");
		String moneystr="000000000000"+money;
		dealmoney=moneystr.substring(moneystr.length()-12);
		return dealmoney;
	}
	public static void setdistribution_typeMap(Map<String,Object> map){
		String distribution_type=String.valueOf(map.get("distribution_type"));
		if("1".equals(distribution_type)||"2".equals(distribution_type)){
			map.put("mer_type","99");
		}else if("3".equals(distribution_type)||"4".equals(distribution_type)){
			map.put("mer_type","99");
			map.put("card_type","99");
		}
	}


	/**
	 *
	 *********************************************************.<br>
	 * [方法] createRandomString <br>
	 * [描述] 获取指定长度随机字符串 <br>
	 * [参数] (对参数的描述) <br>
	 * [返回] String <br>
	 * [时间] 2015-6-16 上午09:35:12 <br>
	 *********************************************************.<br>
	 */
	public static synchronized String createRandomString(int length) {
		//最后又重复两个0和1，因为需要凑足数组长度为64
		char ch[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
		Random random = new Random();
		if (length > 0) {
			int index = 0;
			char[] temp = new char[length];
			int num = random.nextInt();
			for (int i = 0; i < length % 5; i++) {
				temp[index++] = ch[num & 15];
				num >>= 6;
			}
			for (int i = 0; i < length / 5; i++) {
				num = random.nextInt();
				for (int j = 0; j < 5; j++) {
					temp[index++] = ch[num & 15];
					num >>= 6;
				}
			}
			return new String(temp, 0, length);
		}else if (length == 0) {
			return "";
		}else {
			throw new IllegalArgumentException();
		}
	}

	public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || "".equals(hexString)) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}


	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}
	/**
	 *@描述 下载excel模板共有
	 *@参数
	 *@返回值
	 *@创建人  [WGG]
	 *@创建时间 2019-06-13
	 *@修改人和其它信息
	 */
	public static void cnmonDownLoad(InputStream inputStream,String filename, HttpServletResponse response) throws Exception{
		BufferedInputStream inStrem = null;
		BufferedOutputStream outStream = null;
		try {
			response.reset();
			response.setCharacterEncoding("utf-8");
			// Content-disposition 告诉浏览器以下载的形式打开
			response.setHeader("Content-disposition", "attachment; filename=" + filename);
			// application/ms-excel;charset=utf-8 告诉浏览器下载的文件是excel
			response.setContentType("application/ms-excel");
			//os = new FileInputStream(inputStream);
			inStrem = new BufferedInputStream(inputStream);
			outStream = new BufferedOutputStream(response.getOutputStream());

			byte[] buffer = new byte[1024];
			int bytesRead = 0;
			while ((bytesRead = inStrem.read(buffer)) != -1) {
				outStream.write(buffer, 0, bytesRead);
			}
			outStream.flush();
		} catch (IOException e) {
			throw  e;
		} finally {
			if(outStream!=null){
				outStream.close();
			}
			if(inputStream !=null){
				inputStream.close();
			}
			if(inStrem!=null){
				inStrem.close();
			}
		}
	}

	/**
	 *@描述  质检上传文件数据格式
	 *@参数   List MultipartFile
	 *@返回值  String 提示前台页面message
	 *@创建人  [WGG]
	 *@创建时间 2019-06-13
	 *@修改人和其它信息
	 */
	public static String checkFileData(List<String[]> list, MultipartFile fileM) throws Exception{
		boolean flag=fileM.getOriginalFilename().endsWith(".xlsx") || fileM.getOriginalFilename().endsWith(".xls");
		if(!flag){
			return "excel文件格式错误";
		}
		if(list.size()<1){
			return "导入失败，数据有误";
		}
		if(list.size()>500){
			return "导入失败，数据不超过500";
		}
		return "ok";
	}

	public static String hiddenCard(String cardnum){
		if(null!=cardnum && !"".equals(cardnum) && !"null".equals(cardnum)){
			cardnum = cardnum.substring(0,6)+"****"+cardnum.substring(cardnum.length()-4);
			return cardnum;
		}else{
			return  "";
		}
	}

	/**
	 * 用于把List<Map>转换成Map<String,Map>形式，便于存入缓存
	 * @author puyiliang
	 * @param keyName 主键属性
	 * @param list 集合
	 * @return 返回对象
	 */
	public static Map mapListToMap(String keyName, List<Map> list){
		Map m = new HashMap<>();
		try {
			for (Map map : list) {
				Object key =  map.get(keyName);
				m.put(key, map);
			}
			return m;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/** 日期加減天數 **/
	public static String operateDate(String dateParam,int day){
		String newdate=dateParam;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date ndate=sdf.parse(dateParam);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(ndate);
			calendar.add(Calendar.DAY_OF_YEAR,day);
			newdate=sdf.format(calendar.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newdate;
	}

	/**
	 * 用UUID生成十六位数唯一订单号
	 * 生成16位唯一性的订单号s
	 */
	public String getUUID(String agent_num,String type) {
		SimpleDateFormat simpleDateFormat;
		simpleDateFormat = new SimpleDateFormat("yyMMddHHmm");
		Date date = new Date();
		String str = simpleDateFormat.format(date);  //当前时间字符串
		// 随机生成一位整数
		int random = (int) (Math.random() * 9 + 1);
		String valueOf = String.valueOf(random);
		// 生成uuid的hashCode值
		int hashCode = UUID.randomUUID().toString().hashCode();
		// 可能为负数
		if (hashCode < 0) {
			hashCode = -hashCode;
		}
		// 0 代表前面补充0
		// 4 代表长度为4
		// d 代表参数为正数型
		String value = null;
		if(!StringUtils.isEmpty(type)) {
			 value = agent_num + type + valueOf + String.format("%015d", hashCode);
		}else{
			value = agent_num + str + valueOf + String.format("%015d", hashCode);
		}
		//System.out.println("value:"+value);
		return value;
		//
	}
	public static String dealPhone(String linkPhone) {
		if (linkPhone != null && !"".equals(linkPhone.toString())) {
			String panStr = linkPhone.toString().substring(0, 3) + "****" + linkPhone.toString().substring(linkPhone.toString().length() - 4);
			return panStr;
		}
		return linkPhone;
	}

	public static String dealCard(String param) {
		if (param != null && !"".equals(param.toString())) {
			String panStr = param.toString().substring(0, 6) + "****" + param.toString().substring(param.toString().length() - 4);
			return panStr;
		}
		return param;
	}

	/**
	 * @Author: dongjb
	 * @Date: 2019/6/25 1:35
	 * @Description:   对身份证号、银行卡号、手机号加密反Map
	 * @Version: 1.0
	 */
	public static Map<String, String> encryption(String idCard, String bankCard,String phone){
		Map<String, String> map = Maps.newHashMap();
		try {
			if(isNull(bankCard)){
				bankCard="";
			}else{
				map.put("bankCardEn", EncryptUtils.getEncrypt(bankCard));
				bankCard = dealCard(bankCard);
			}
			map.put("bankCard", bankCard);

			if(isNull(idCard)){
				idCard="";
			}else{
				map.put("idCardEn",EncryptUtils.getEncrypt(idCard));
				idCard= dealCard(idCard);
			}
			map.put("idCard", idCard);

			if(isNull(phone)){
				phone="";
			}else{
				map.put("phoneEn",EncryptUtils.getEncrypt(phone));
				phone = dealPhone(phone);
			}
			map.put("phone", phone);
		} catch (Exception e) {
			//账户信息加密异常
		}
		return map;
	}
	public static String getStringRandom(int length) {
		String val = "";
		Random random = new Random();
		//length为几位密码
		for(int i = 0; i < length; i++) {
			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
			//输出字母还是数字
			if( "char".equalsIgnoreCase(charOrNum) ) {
				//输出是大写字母还是小写字母
//                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
				int temp =97;
				val += (char)(random.nextInt(26) + temp);
			} else if( "num".equalsIgnoreCase(charOrNum) ) {
				val += String.valueOf(random.nextInt(10));
			}
		}
		return val;
	}

    /**
     * 将Clob转为String
     *
     * @param clob
     * @return
     * @throws Exception
     */
	public static String clobToString(Clob clob) throws Exception {
		Reader is=null;
		BufferedReader br=null;
		String reString = "";
		try{
            // 得到流
			is = clob.getCharacterStream();
			br = new BufferedReader(is);
			String s = br.readLine();
			StringBuffer sb = new StringBuffer();
            // 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
			while (s != null) {
				sb.append(s);
				s = br.readLine();
			}
			reString = sb.toString();

			return reString;
		}catch (Exception e){
			throw e;
		}finally {
			is.close();
			br.close();
		}
	}

	/**
	 * String null 转 ""
	 * @param obj
	 * @return boolean
	 */
	public static String castStringNullToEmpty(String value) {
		if (value == null || "null".equals(toLowerCase(value))){
			value = " ";
		}
		return value;
	}

	/**
	 * 将字符串中大写字母转小写字母
	 * @param str
	 * @return
	 */
	public static String toLowerCase(String str) {
		char[] s=str.toCharArray();
		for(int i=0;i<s.length;i++){
			if(s[i]>='A' && s[i]<='Z'){
				s[i]+=32;
			}
		}
		str="";
		for(int j=0;j<s.length;j++){
			str+=s[j];
		}
		return str;
	}

	/**
	 * 判断一个Object 是否能转数字
	 * @param obj
	 * @return boolean
	 */
	public static boolean isNumber (Object obj) {
		if (obj == null){
			return false;
		}
		if (obj instanceof Number) {
			return true;
		} else if (obj instanceof String){
			try{
				Double.parseDouble((String)obj);
				return true;
			}catch (Exception e) {
				return false;
			}
		}
		return false;
	}

	public static String getDsettleSerial(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String date = "XYR"+sdf.format(new Date());
		return date+getDtranFlow();
	}
	public static synchronized String getDtranFlow(){
		try {
			Thread.sleep(50);
		} catch (Exception e) {
		}
		long timeStamp = System.currentTimeMillis();
		long timeStamp1 = System.currentTimeMillis();
		long dayStamp = timeStamp*timeStamp1;
		String trStr = dayStamp+"";
		return trStr.substring(trStr.length()-12);
	}

	public static String fileCopy(MultipartFile file) throws Exception{
		//文件复制
		String fileUrl = AutoLoadData.filetemp + file.getOriginalFilename();
		File newFile=new File(fileUrl);
		if (newFile.getParentFile() != null && !newFile.getParentFile().exists()) {
			newFile.getParentFile().mkdirs();
		}
		if(!newFile.exists()){
			newFile.createNewFile();
		}
		file.transferTo(newFile);
		return fileUrl;
	}
	public static File fileCopyNew(MultipartFile file) throws Exception {
		//获取上传的文件名
		String fileName = file.getOriginalFilename();
		// 获取文件后缀名
		String suffix=fileName.substring(fileName.lastIndexOf("."));
		// 用uuid作为文件名，防止生成的临时文件重复
		File dest = File.createTempFile(IdUtil.randomUUID(), suffix);
		// 将上传文件复制到临时文件
		FileUtils.copyInputStreamToFile(file.getInputStream(),dest);
		return dest;
	}

	/**
	 * 金额正则验证
	 * @Param [str]
	 * @Return boolean
	 */
	public static boolean isNotMoney(String str){
		Pattern pattern= Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达式
		Matcher match=pattern.matcher(str);
		if(match.matches()==false){
			return false;
		} else{
			return true;
		}
	}
}
